﻿using DBUtility;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Common;

namespace Business
{
    public class PAY_ORDER
    {
        #region http上下文
        public static HttpContext Current
        {
            get { return HttpContext.Current; }
        }

        public static HttpRequest Request
        {
            get { return Current.Request; }
        }

        public static HttpResponse Response
        {
            get { return Current.Response; }
        }
        #endregion
        #region model
        public int id { get; set; }                
        public string order_no { get; set; }
        public string username { get; set; }
        public decimal amount { get; set; }
        public string pay_type { get; set; }//支付渠道
        public string state { get; set; }//估计 是否充值成功 N等待充值；Y充值成功
        public string addtime { get; set; }//时间戳
        public string trade_no { get; set; }//第三方订单号
        public string trade_time { get; set; }//时间戳
        public string is_check { get; set; }//估计 是否加钱成功 N等待加钱；Y加钱成功
        public string dingdong { get; set; }
        public string yichang { get; set; }
        public string yc_time { get; set; }//时间戳
        #endregion

        #region Function
        public PAY_ORDER() { }
        public PAY_ORDER Get(int ID)
        {
            PAY_ORDER PAY_ORDER = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ORDER where ID={0}", ID)))
            {
                if (sdr.Read())
                {
                    PAY_ORDER = GetData(PAY_ORDER, sdr);
                }
            }
            sdr.Close();
            return PAY_ORDER;
        }
        public PAY_ORDER SelectByInt(String where, int value)
        {
            PAY_ORDER PAY_ORDER = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ORDER where {0}={1} ", where, value)))
            {
                while (sdr.Read())
                {
                    PAY_ORDER = GetData(PAY_ORDER, sdr);
                }
            }
            sdr.Close();
            return PAY_ORDER;
        }
        public PAY_ORDER SelectByStr(String where, String value)
        {
            PAY_ORDER PAY_ORDER = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ORDER where {0}='{1}' ", where, value)))
            {
                while (sdr.Read())
                {
                    PAY_ORDER = GetData(PAY_ORDER, sdr);
                }
            }
            sdr.Close();
            return PAY_ORDER;
        }
        public PAY_ORDER SelectByObj(String where)
        {
            PAY_ORDER PAY_ORDER = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ORDER where 1=1 and {0} ", where)))
            {
                while (sdr.Read())
                {
                    PAY_ORDER = GetData(PAY_ORDER, sdr);
                }
            }
            sdr.Close();
            return PAY_ORDER;
        }
        public List<PAY_ORDER> GetALL()
        {
            PAY_ORDER PAY_ORDER = null;
            MySqlDataReader sdr = null;
            List<PAY_ORDER> list = new List<PAY_ORDER>();
            using (sdr = MYSQL.ExecuteReader("select * From PAY_ORDER"))
            {
                while (sdr.Read())
                {
                    PAY_ORDER = GetData(PAY_ORDER, sdr);
                    list.Add(PAY_ORDER);
                }
            }
            sdr.Close();
            return list;
        }
        public List<PAY_ORDER> GetALL(String where)
        {
            PAY_ORDER PAY_ORDER = null;
            MySqlDataReader sdr = null;
            List<PAY_ORDER> list = new List<PAY_ORDER>();
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_ORDER where 1=1 and {0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_ORDER = GetData(PAY_ORDER, sdr);
                    list.Add(PAY_ORDER);
                }
            }
            sdr.Close();
            return list;
        }
        /// <summary>
        /// 获取前几个
        /// </summary>
        /// <param name="where"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<PAY_ORDER> GetALL(String where, int top)
        {
            PAY_ORDER PAY_ORDER = null;
            MySqlDataReader sdr = null;
            List<PAY_ORDER> list = new List<PAY_ORDER>();
            using (sdr = MYSQL.ExecuteReader(string.Format("select top " + top + " * From PAY_ORDER where 1=1 and {0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_ORDER = GetData(PAY_ORDER, sdr);
                    list.Add(PAY_ORDER);
                }
            }
            sdr.Close();
            return list;
        }
        public List<PAY_ORDER> GetList(String where)
        {
            PAY_ORDER PAY_ORDER = null;
            MySqlDataReader sdr = null;
            List<PAY_ORDER> list = new List<PAY_ORDER>();
            using (sdr = MYSQL.ExecuteReader(string.Format("{0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_ORDER = GetData(PAY_ORDER, sdr);
                    list.Add(PAY_ORDER);
                }
            }
            sdr.Close();
            return list;
        }
        public bool Insert(ref PAY_ORDER PAY_ORDER)
        {
            try
            {
                Current.Application.Lock();
                MySqlParameter[] p = new MySqlParameter[]{
                new MySqlParameter("@order_no",PAY_ORDER.order_no),
                new MySqlParameter("@username",PAY_ORDER.username),
                new MySqlParameter("@amount",PAY_ORDER.amount),
                new MySqlParameter("@pay_type",PAY_ORDER.pay_type),
                new MySqlParameter("@state",PAY_ORDER.state),
                new MySqlParameter("@addtime",StringHelper.ConvertDateTimeToStamp(PAY_ORDER.addtime)),
                new MySqlParameter("@trade_no",PAY_ORDER.trade_no),
                new MySqlParameter("@trade_time",StringHelper.ConvertDateTimeToStamp(PAY_ORDER.trade_time)),
                new MySqlParameter("@is_check",PAY_ORDER.is_check),
                new MySqlParameter("@dingdong",PAY_ORDER.dingdong),
                new MySqlParameter("@yichang",PAY_ORDER.yichang),
                new MySqlParameter("@yc_time",StringHelper.ConvertDateTimeToStamp(PAY_ORDER.yc_time)),
            };
                
                int i = Convert.ToInt32(MYSQL.ExecuteSql(string.Format("insert into PAY_ORDER values(null,@order_no,@username,@amount,@pay_type,@state,@addtime,@trade_no,@trade_time,@is_check,@dingdong,@yichang,@yc_time)"), p));
                return i == 0 ? false : true;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                Current.Application.UnLock();
            }
        }
        public bool Update(PAY_ORDER PAY_ORDER)
        {
            MySqlParameter[] p = new MySqlParameter[]{
                new MySqlParameter("@ID",PAY_ORDER.id),
                new MySqlParameter("@order_no",PAY_ORDER.order_no),
                new MySqlParameter("@username",PAY_ORDER.username),
                new MySqlParameter("@amount",PAY_ORDER.amount),
                new MySqlParameter("@pay_type",PAY_ORDER.pay_type),
                new MySqlParameter("@state",PAY_ORDER.state),
                new MySqlParameter("@addtime",StringHelper.ConvertDateTimeToStamp(PAY_ORDER.addtime)),
                new MySqlParameter("@trade_no",PAY_ORDER.trade_no),
                new MySqlParameter("@trade_time",StringHelper.ConvertDateTimeToStamp(PAY_ORDER.trade_time)),
                new MySqlParameter("@is_check",PAY_ORDER.is_check),
                new MySqlParameter("@dingdong",PAY_ORDER.dingdong),
                new MySqlParameter("@yichang",PAY_ORDER.yichang),
                new MySqlParameter("@yc_time",StringHelper.ConvertDateTimeToStamp(PAY_ORDER.yc_time))
            };
            int i = MYSQL.ExecuteSql(string.Format("update PAY_ORDER set ID=@ID,order_no=@order_no,username=@username,amount=@amount,pay_type=@pay_type,state=@state,addtime=@addtime,trade_no=@trade_no,trade_time=@trade_time,is_check=@is_check,dingdong=@dingdong,yichang=@yichang,yc_time=@yc_time where ID=@ID"),p);
            return i == 0 ? false : true;
        }
        public bool Delete(int ID)
        {
            int i = MYSQL.ExecuteSql(string.Format("delete from PAY_ORDER where ID={0}", ID));
            return i == 0 ? false : true;
        }
        private PAY_ORDER GetData(PAY_ORDER PAY_ORDER, MySqlDataReader sdr)
        {
            PAY_ORDER = new PAY_ORDER();
            PAY_ORDER.id = Convert.ToInt32(sdr["ID"]);
            PAY_ORDER.order_no = sdr["order_no"].ToString();
            PAY_ORDER.username = sdr["username"].ToString();
            PAY_ORDER.amount = Convert.ToDecimal(sdr["amount"]);
            PAY_ORDER.pay_type = sdr["pay_type"].ToString();
            PAY_ORDER.state = sdr["state"].ToString();
            PAY_ORDER.addtime = StringHelper.ConvertStampToDateTime(sdr["addtime"].ToString());
            PAY_ORDER.trade_no = sdr["trade_no"].ToString();
            PAY_ORDER.trade_time = StringHelper.ConvertStampToDateTime(sdr["trade_time"].ToString());  
            PAY_ORDER.is_check = sdr["is_check"].ToString();
            PAY_ORDER.dingdong = sdr["dingdong"].ToString();
            PAY_ORDER.yichang = sdr["yichang"].ToString();
            PAY_ORDER.yc_time = StringHelper.ConvertStampToDateTime(sdr["yc_time"].ToString());
            return PAY_ORDER;
        }
        /// <summary>
        /// 将DataTable数据转换成实体类列表
        /// 郑科 
        /// 日期：2016年11月4日10.18
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>List<School></returns>
        public List<PAY_ORDER> ParseToList(DataTable dt)
        {
            if (dt == null || dt.Rows.Count < 1)
            {
                return null;
            }
            List<PAY_ORDER> PAY_ORDER_list = new List<PAY_ORDER>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PAY_ORDER PAY_ORDER = new PAY_ORDER();
                PAY_ORDER.id = int.Parse(dt.Rows[i]["ID"].ToString());
                PAY_ORDER.order_no = dt.Rows[i]["order_no"].ToString();
                PAY_ORDER.username = dt.Rows[i]["username"].ToString();
                PAY_ORDER.amount = Convert.ToDecimal(dt.Rows[i]["amount"].ToString());
                PAY_ORDER.pay_type = dt.Rows[i]["pay_type"].ToString();
                PAY_ORDER.state = dt.Rows[i]["state"].ToString();
                PAY_ORDER.addtime = StringHelper.ConvertStampToDateTime(dt.Rows[i]["addtime"].ToString());
                PAY_ORDER.trade_no = dt.Rows[i]["trade_no"].ToString();
                PAY_ORDER.trade_time = StringHelper.ConvertStampToDateTime(dt.Rows[i]["trade_time"].ToString());
                PAY_ORDER.is_check = dt.Rows[i]["is_check"].ToString();
                PAY_ORDER.dingdong = dt.Rows[i]["dingdong"].ToString();
                PAY_ORDER.yichang = dt.Rows[i]["yichang"].ToString();
                PAY_ORDER.yc_time = StringHelper.ConvertStampToDateTime(dt.Rows[i]["yc_time"].ToString());
                PAY_ORDER_list.Add(PAY_ORDER);
            }
            return PAY_ORDER_list;
        }
        #endregion
        #region join
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageindex">第几页</param>
        /// <param name="pagesize">一页列表数量</param>
        /// <param name="recordcount">记录总数</param>
        /// <returns></returns>
        public List<PAY_ORDER> PagingQueryList(PageTable pageTable, out int recordcount)
        {
            recordcount = MYSQL.GetInt("select count(*) from PAY_ORDER where " + pageTable.Where);
            return ParseToList(MYSQL.GetDataTable(PageSql.GetPage(pageTable)));
        }
        #endregion
    }
}
